/*
** drone.c for zappy in /home/guidet_m/TECK2/projets/zappy/server_zappy
** 
** Made by michael guidet
** Login   <guidet_m@epitech.net>
** 
** Started on  Mon Jul  4 15:39:54 2011 michael guidet
** Last update Mon Jul  4 15:40:27 2011 michael guidet
*/

#include "server.h"

t_player		*new_drone(char *team, int x, int y, int t)
{
  t_player	*new_elem;

  new_elem = x_null(NULL, malloc(sizeof(*new_elem)), "malloc");
  new_elem->x = x;
  new_elem->y = y;
  new_elem->look_at = (rand() % 4) * 2 + 1;
  new_elem->team = team;
  new_elem->level = 1;
  new_elem->life = (int)(1260.0f / (float)t * 1000000.0f);
  new_elem->ressource[NOURRITURE] = 10;
  new_elem->ressource[LINEMATE] = 0;
  new_elem->ressource[DERAUMERE] = 0;
  new_elem->ressource[SIBUR] = 0;
  new_elem->ressource[MENDIANE] = 0;
  new_elem->ressource[PHIRAS] = 0;
  new_elem->ressource[THYSTAME] = 0;
  return (new_elem);
}

void		add_drone_to_team(t_team *team, t_player *drone)
{
  t_player_list	*new_elem;

  new_elem = x_null(NULL, malloc(sizeof(*new_elem)), "malloc");
  new_elem->drone = drone;
  new_elem->next = team->ia_players;
  team->ia_players = new_elem;
}

void		add_drone_to_case(t_case **map, int x, int y, t_player *drone)
{
  t_player_list	*new_elem;

  new_elem = x_null(NULL, malloc(sizeof(*new_elem)), "malloc");
  new_elem->drone = drone;
  new_elem->next = map[y][x].players;
  map[y][x].players = new_elem;
}

void		remove_drone_from_team(t_team *team, t_player *drone)
{
  t_player_list	*tmp;
  t_player_list	*to_free;

  if (team->ia_players->drone == drone)
    {
      to_free = team->ia_players;
      team->ia_players = team->ia_players->next;
      free(to_free);
      return ;
    }
  for (tmp = team->ia_players; tmp->next; tmp = tmp->next)
    if (tmp->next->drone == drone)
      break;
  to_free = tmp->next;
  tmp->next = tmp->next->next;
  free(to_free);
}

void		remove_drone_from_case(t_case **map, int x, int y, t_player *drone)
{
  t_player_list	*tmp;
  t_player_list	*to_free;

  if (map[y][x].players->drone == drone)
    {
      to_free = map[y][x].players;
      map[y][x].players = map[y][x].players->next;
      free(to_free);
      return ;
    }
  for (tmp = map[y][x].players; tmp->next; tmp = tmp->next)
    if (tmp->next->drone == drone)
      break;
  if (!tmp->next)
    exit(EXIT_FAILURE);
  to_free = tmp->next;
  tmp->next = tmp->next->next;
  free(to_free);
}
